import math
import sys
import scipy.optimize
from refprop2 import *


def density_T_P(t,p,fluid):
    SETUP(fluid)
    return(TPFLSH(t,p)[0]*wm.value)

def density_H_P(h,p,fluid,setup=False):
    if setup:
        SETUP(fluid)
    return(PHFLSH(p,h)[1]*wm.value)

def enthalpy_T_P(t,p,fluid=False,setup=False):
    if setup:
        SETUP(fluid)
    return(TPFLSH(t,p)[5]/wm.value)
    
def temperature_P_H(p,h,fluid=False,setup=False):    
    if setup and fluid:
        SETUP(fluid)
    return(PHFLSH(p,h*wm.value)[0])    

def pressure_rho_h(r,h,fluid,setup=False):
    if setup:
        SETUP(fluid)
    r = r / wm.value
    h = h * wm.value
    return(DHFLSH(r,h)[1])

def props_D_H(r,h,fluid,setup=False):
    if setup:
        SETUP(fluid)
    r = r / wm.value
    h = h * wm.value
    return(DHFLSH(r,h))
#densityDown_sat_throttle_T0
def denThrottle_Tup_Pdown(T0,p1,fluid):
    SETUP(fluid)
    denUp = SATT(T0)[1]*wm.value
    h = ENTHAL(T0,denUp/wm.value)/wm.value
    tDown = SATP(p1)[0]
    return(THFLSH(tDown,h*wm.value)[1]*wm.value)

def denThrottle_Hup_Pdown(h0,p1,fluid):
    SETUP(fluid)
    tDown = SATP(p1)[0]
    return(THFLSH(tDown,h0*wm.value)[1]*wm.value)

def enthalpy_saturation_T(T0,fluid):
    SETUP(fluid)
    den = SATT(T0)[1]
    return(ENTHAL(T0,den)/wm.value)

def myfrange(start, stop, n):
    L = [0.0] * n
    nm1 = n - 1
    nm1inv = 1.0 / nm1
    for i in range(n):
        L[i] = nm1inv * (start*(nm1 - i) + stop*i)
    return L

